package dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.liudecai.utils.jdbc.TxQueryRunner;

import bean.RequestBean;

public class Request2BeanDao {
	QueryRunner qr=null;
	public Request2BeanDao() {
		qr = new TxQueryRunner();
	}
	public void Add(RequestBean reqbean) throws SQLException {
		String sql = "insert into test_req2bean(username,password,telephone) values(?,?,?)";
		qr.update(sql,reqbean.getUsername(),reqbean.getPassword(),reqbean.getTelephone());
	}

	public void Delete(int id) throws SQLException {
		String sql = "delete from test_req2bean where id=?";
		qr.update(sql,id);
	}

	public void Update(RequestBean reqbean) throws SQLException {
		String sql = "update test_req2bean set username=?,password=?,telephone=? where id=?";
		qr.update(sql, reqbean.getUsername(),reqbean.getPassword(),reqbean.getTelephone(),reqbean.getId());
	}

	public RequestBean FindById(int id) throws SQLException {
		String sql = "select * from test_req2bean where id=?";
		RequestBean reqbean = qr.query(sql, new BeanHandler<RequestBean>(RequestBean.class),id);
		return reqbean;

	}

	public List<RequestBean> FindByString(String str) throws SQLException {
		String sql="select * from test_req2bean N where Concat(N.*) like '%"+str+"%'";
		List<RequestBean> reqbean = qr.query(sql, new BeanListHandler<RequestBean>(RequestBean.class));
		return reqbean;
		
	}

	public List<RequestBean> FindAll() throws SQLException {
		String sql="select * from test_req2bean";
		List<RequestBean> reqbean = qr.query(sql, new BeanListHandler<RequestBean>(RequestBean.class));
		return reqbean;
	}

	public long RequestBeanCount() throws SQLException {
		String sql="select count(id) from test_req2bean";
		return qr.query(sql, new ScalarHandler<Long>());
	}
}
